package org.dbstar.scheduler.control.db.dependency;

import java.sql.Types;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONSerializer;

import org.apache.commons.lang.StringUtils;
import org.dbstar.scheduler.beans.JobDependency;
import org.dbstar.scheduler.beans.JobDependencyItem;
import org.dbstar.scheduler.identifier.Identifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;


/**
 * 快速插入JobDependency对象
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-6-14
 */
public class JobDependencyInsert extends SimpleJdbcInsert {
	public JobDependencyInsert(JdbcTemplate jdbcTemplate) {
		super(jdbcTemplate);
		withTableName("SCHD_JOB_DEPENDENCY");
		usingColumns("SYSTEM_IDENTIFIER", "SEQUENCE", "IDENTIFIER", "CONF_SYSTEM_IDENTIFIER", "CONF_SEQUENCE",
				"CONF_IDENTIFIER", "NAME", "DESCRIPTION", "KEY_SIZE", "KEY_DATA", "PARAMETER_SIZE", "PARAMETER_DATA",
				"DEPENDENCY_SIZE", "DEPENDENCY_DATA", "CREATE_TIME", "OWNER");
	}

	public int insertJobDependency(JobDependency dep, Identifier identifier) {
		MapSqlParameterSource source = new MapSqlParameterSource();
		source.addValue("SYSTEM_IDENTIFIER", identifier.getSystemIdentifier(), Types.VARCHAR);
		source.addValue("SEQUENCE", identifier.getSequence(), Types.INTEGER);
		source.addValue("IDENTIFIER", identifier.getStringIdentifier(), Types.VARCHAR);
		source.addValue("CONF_SYSTEM_IDENTIFIER", dep.getJobConfIdentifier().getSystemIdentifier(), Types.VARCHAR);
		source.addValue("CONF_SEQUENCE", dep.getJobConfIdentifier().getSequence(), Types.INTEGER);
		source.addValue("CONF_IDENTIFIER", dep.getJobConfIdentifier().getStringIdentifier(), Types.VARCHAR);
		source.addValue("NAME", dep.getName(), Types.VARCHAR);
		source.addValue("DESCRIPTION", dep.getDescription(), Types.VARCHAR);
		source.addValue("KEY_SIZE", dep.getKeySize(), Types.INTEGER);
		source.addValue("KEY_DATA", parseKeysJson(dep.getKeys()), Types.VARCHAR);
		source.addValue("PARAMETER_SIZE", dep.getParameterSize(), Types.INTEGER);
		source.addValue("PARAMETER_DATA", parseParametersJson(dep.getParameters()), Types.VARCHAR);
		source.addValue("DEPENDENCY_SIZE", dep.getDependencySize(), Types.INTEGER);
		source.addValue("DEPENDENCY_DATA", parseDependenciesJson(dep.getDependencies()), Types.VARCHAR);
		source.addValue("CREATE_TIME", dep.getCreateTime(), Types.BIGINT);
		source.addValue("OWNER", dep.getOwner(), Types.VARCHAR);
		return super.execute(source);
	}

	private String parseKeysJson(List<String> keys) {
		if (keys == null || keys.isEmpty()) {
			return null;
		} else {
			return StringUtils.join(keys, ',');
		}
	}

	private String parseParametersJson(Map<String, String> parameters) {
		if (parameters == null || parameters.isEmpty()) {
			return null;
		} else {
			return JSONSerializer.toJSON(parameters).toString();
		}
	}

	private String parseDependenciesJson(Set<JobDependencyItem> dependencies) {
		if (dependencies == null || dependencies.isEmpty()) {
			return null;
		} else {
			return JSONSerializer.toJSON(dependencies, new JobDependencyItemJsonConfig()).toString();
		}
	}
}
